<?php
class chatController extends  appController{
	
	public function index(){
			$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
// 			$update=isset($_REQUEST['page'])?false:true;
			$list=Model('chat')->listUnchecked($page);
			$this->assign('sql_list',$list['data']);
			$this->assign('list_page',$list['pagelink']);
			$this->assign('count',$list['count']);
			$this->assign('passCount',$list['passCount']);
			$this->assign('page',$page);
			$staggerTime = intval(Model('chat')->get_cfg("staggerTime"));
			//uvpv
			$visitortime = Model('chat')->get_cfg("visitortime");
			$visitornow = time()+$staggerTime-$visitortime;
			$vtime =strtotime(date('Y-m-d H:00:00',$visitornow));
			//获取uv与pv
			$result = Model("chat")->get_uv_pv_2M($visitornow,$vtime,$visitortime);
			$this->assign("pv", $result['pv']);
			$this->assign("uv", $result['uv']);
			
			//获取上一周期弹幕池数量
			$lastCycleNum = Model('chat')->get_cfg("lastCycleNum");
			$this->assign("lastCycleNum",$lastCycleNum);
			
			//刷新时间
			$timestamp =intval(Model('chat')->get_timestamp());
			$refresh = intval(Model('chat')->get_refreshtime());
			$status = intval(Model('chat')->get_status_pass());
			$time = time();
			$this->assign('timeout',($refresh-($time-($timestamp-$staggerTime))%$refresh));//
			$this->assign('refreshtime',$refresh);
			$this->assign('status',$status);
			$this->assign("timestamp", $timestamp-$staggerTime);
			$this->assign("now", $time);
			
			
			
			
			$this->display('index.html');
	}
	
	public function batchPass(){
		$uids = $_POST['uids'];
		$result=array('error'=>0,'content'=>'批量审核通过失败');
		if(Model('chat')->updateStatus($uids,3)>0){
			$result['error'] = 1;
			$result['content'] = "批量审核通过成功";
		}
		print_r(json_encode($result));
		exit();
	}
	public function batchNopass(){
		$uids = $_POST['uids'];
		$result=array('error'=>0,'content'=>'批量审核不通过失败');
		if(Model('chat')->updateStatus($uids,2)>0){
			$result['error'] = 1;
			$result['content'] = "批量审核不通过成功";
		}
		print_r(json_encode($result));
		exit();
	}
	public function cleanChatPool(){
		$result=array('error'=>0,'content'=>'清除成功');
		Model("chat")->cleanChatPool();
		$fp = fopen(ROOT_PATH."chat.json", "w");
		fwrite($fp, json_encode(array()));
		fclose($fp);
		print_r(json_encode($result));
	}
	public function cleanNoCheck(){
		$result = Model("chat")->cleanNoCheck();
		print_r(json_encode($result));
	}
	public function update(){
	$status = isset($_GET['status'])?intval($_GET['status']):0;
		if(!$status){
			Model('chat')->updateComment();
		}else{
			Model('chat')->updateCommentPass();
		}
		$result['error'] = 1;
		$result['content'] = "刷新成功";
		print_r(json_encode($result));
		exit();
	}
	public function updatestatus(){
		$status = isset($_GET['status'])?intval($_GET['status']):0;
		$status = Model('chat')->update_pass_status($status);
		echo $status;
	}
	/**
	 * 特殊弹幕
	 */
	public function specialComment(){
		
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$special_list=Model('chat')->special_list($page);
		$this->assign('ur_here','>特殊弹幕管理');
		//$this->assign('search_action','index.php?g=admin&m=ad&a=search_group');
		$this->assign('special_list',$special_list['data']);
		$this->assign('user_list_page',$special_list['pagelink']);
		$this->display('special_list.html');
	}
	
	public function add_chat(){
		$this->assign('ur_here','>新增特殊项弹幕');
		$this->assign('action','create_chat');
		$group = Model('chat')->getGroups($id);
		
		//var_dump($chat);
		$this->assign('group',$group);
		$this->display('special_edit.html');
		
	}
	public function create_chat(){
		$info['title']=isset($_POST['title'])?$_POST['title']:'';
		$info['subtitle']=isset($_POST['subtitle'])?$_POST['subtitle']:'';
		$info['group_id']=isset($_POST['group_id'])?$_POST['group_id']:'0';
		$info['source_url']=isset($_POST['source_url'])?$_POST['source_url']:'';
		$info['pic_url']=isset($_POST['pic_url'])?$_POST['pic_url']:'';
		$info['background_color']=isset($_POST['background_color'])?$_POST['background_color']:'';
		$info['color']=isset($_POST['color'])?$_POST['color']:'';
		$info['addtime'] = $_SERVER['REQUEST_TIME'];
		$result=array('error'=>1,'content'=>'');
		if($_POST['title']=='')
		{
			$data['error']=1;
			$data['content']=Lang('标题不能为空');
			$this->showMsg($data);
		}
		if( mb_strlen($_POST['title'],'UTF8')>15){
			$data['error']=1;
			$data['content']=Lang('标题长度不可超过14字');
			$this->showMsg($data);
		}
		  if($ad_id=Model('chat')->insert($info,'chat_special'))
		  {
		  	   $result['error']=0;
			  $result['content']=Lang('SUCCESS_ADD');
		  }
		  else
		  {
				$result['content']=Lang('INVALID_OPERATION');
		  }
		$this->showMsg($result);
	}
	public function edit_chat(){
		$this->assign('ur_here','>编辑特殊项弹幕');
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		$chat=Model('chat')->get_chat_special($id);
		
		$group = Model('chat')->getGroups($id);
		
		//var_dump($chat);
		$this->assign('group',$group);
		$this->assign('chat',$chat);
		$this->assign('id',$id);
		$this->assign('action','update_chat');
		$this->display('special_edit.html');
	}
	public function update_chat(){
		$id=isset($_POST['id'])?$_POST['id']:'';
		$info['title']=isset($_POST['title'])?$_POST['title']:'';
		$info['group_id']=isset($_POST['group_id'])?$_POST['group_id']:'0';
		$info['subtitle']=isset($_POST['subtitle'])?$_POST['subtitle']:'';
		$info['source_url']=isset($_POST['source_url'])?$_POST['source_url']:'';
		$info['background_color']=isset($_POST['background_color'])?$_POST['background_color']:'';

		$info['color']=isset($_POST['color'])?$_POST['color']:'color1';
			
		$info['pic_url']=isset($_POST['pic_url'])?$_POST['pic_url']:'';
		$data=array('error'=>1,'content'=>'');
		if($_POST['title']=='')
		{
			$data['error']=1;
			$data['content']=Lang('标题不能为空');
			$this->showMsg($data);
		}
		if( mb_strlen($_POST['title'],'UTF8')>15){
			$data['error']=1;
			$data['content']=Lang('标题长度不可超过14字');
			$this->showMsg($data);
		}
		$result=Model('chat')->update($info,array('table'=>'chat_special','id'=>$id));
		if($result)
		{
			$data['error']=0;
			$data['content']=Lang('SUCCESS_EDIT');
				
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	}
	public function del_chat()
	{
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('chat')->del_chat($v);
			}
		}
		else
		{
			$result=Model('chat')->del_chat($id);
		}
		print_r(json_encode($result));
	}
	public function changColor(){
		$id=isset($_POST['id'])?$_POST['id']:'';
		$color = isset($_POST['color'])?$_POST['color']:'';
		$data=array('error'=>1);
		if($id&&$color){
			if(Model('chat')->changColor($id,$color)){
				$data=array('error'=>0);
			}
		}
		print_r(json_encode($data));
	}
	/***********************************************以下是规则管理的方法****************************************************************/
	public function rule_list(){
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$special_id=isset($_GET['bid'])?intval($_GET['bid']):'';
		$rule_list=Model('chat')->rule_list($page,$special_id);
		$this->assign('special_id',$special_id);
		$this->assign('rule_list',$rule_list['data']);
		$this->assign('user_list_page',$rule_list['pagelink']);
		$this->assign('count',$rule_list['count']);
		$this->display('rule_list.html');
	}
	public function addRule(){
		$sid=isset($_GET['sid'])?intval($_GET['sid']):'';
		$this->assign('sid',$sid);
		$this->assign('action','create_rule');
		$this->display('rule_edit.html');
	}
	
	public function create_rule(){
		$info['rule_name']=isset($_POST['rule_name'])?$_POST['rule_name']:'';
		$info['status']=isset($_POST['status'])?intval($_POST['status']):1;
		$info['time']=isset($_POST['time'])?intval($_POST['time']):1;
		$info['comment_num']=isset($_POST['comment_num'])?intval($_POST['comment_num']):0;
		$info['showtime']=isset($_POST['showtime'])?intval($_POST['showtime']):0;
		$info['special_id']=isset($_POST['sid'])?$_POST['sid']:0;
		$info['specific_time']=isset($_POST['specific_time'])?strtotime($_POST['specific_time']):0;
		$info['addtime'] = $_SERVER['REQUEST_TIME'];
		$data=array('error'=>1,'content'=>'');
		if($_POST['rule_name']=='')
		{
			$data['error']=1;
			$data['content']=Lang('规则名不能为空');
			$this->showMsg($data);
		}
		$result=Model('chat')->insert($info,'chat_special_rule');
		if($result)
		{
			$data['error']=0;
			$data['content']=Lang('SUCCESS_EDIT');
		
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	}
	public function editRule(){
		$this->assign('ur_here','>编辑特殊项弹幕规则');
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		$sid=isset($_GET['sid'])?intval($_GET['sid']):'';
		$rule=Model('chat')->get_chat_special_rule($id);
		$this->assign('sid',$sid);
		$this->assign('rule',$rule);
		$this->assign('id',$id);
		$this->assign('action','update_rule');
		$this->assign('time',date('Y-m-d H:i:s',time()));
		$this->display('rule_edit.html');
	}
	
	public function update_rule(){
		$id=isset($_POST['id'])?$_POST['id']:'';
		$info['rule_name']=isset($_POST['rule_name'])?$_POST['rule_name']:'';
		$info['status']=isset($_POST['status'])?intval($_POST['status']):1;
		$info['time']=isset($_POST['time'])?intval($_POST['time']):1;
		$info['comment_num']=isset($_POST['comment_num'])?intval($_POST['comment_num']):0;
		$info['showtime']=isset($_POST['showtime'])?intval($_POST['showtime']):0;
		$info['special_id']=isset($_POST['sid'])?$_POST['sid']:0;
		$info['specific_time']=isset($_POST['specific_time'])?strtotime($_POST['specific_time']):0;
		$info['addtime'] = $_SERVER['REQUEST_TIME'];
		$data=array('error'=>1,'content'=>'');
		if($_POST['rule_name']=='')
		{
			$data['error']=1;
			$data['content']=Lang('规则名不能为空');
			$this->showMsg($data);
		}
		$result=Model('chat')->update($info,array("table"=>'chat_special_rule',"id"=>$id));
		if($result)
		{
			$data['error']=0;
			$data['content']=Lang('SUCCESS_EDIT');
		
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	}
	
	public function delRule(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('chat')->del_rule($v);
			}
		}
		else
		{
			$result=Model('chat')->del_rule($id);
		}
		print_r(json_encode($result));
	}
	
	/***********************************************以下是评论记录统计的方法****************************************************************/
	public function record(){
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$startTime = isset($_REQUEST['startTime'])?strtotime($_REQUEST['startTime']):mktime(0,0,0,date("m"),date("d"),date("Y"));
		$endTime = isset($_REQUEST['endTime'])?strtotime($_REQUEST['endTime']):0;
		$nickname = isset($_REQUEST['nickname'])?$_REQUEST['nickname']:'';
		$comment = isset($_REQUEST['comment'])?$_REQUEST['comment']:'';
		if($endTime!=0){
			$endTime = strtotime(date( 'Y-m-d 23:59:59', $endTime));
		}else{
			$endTime = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
		}
		$status = isset($_REQUEST['status'])?$_REQUEST['status']:6;
		$total['startTime'] = $startTime;
		$total['endTime'] = $endTime;
		$total['nickname'] = $nickname;
		$total['comment'] = $comment;
		if($status!=6){
			$total['status'] = $status;
		}
		$list=Model('chat')->list_record($page,$total);
		$this->assign('sql_list',$list['data']);
		$this->assign('list_page',$list['pagelink']);
		$total['count'] = $list['count'];
		$total['pass_count'] = $list['pass_count'];
		$total['nopass_count'] = $list['nopass_count'];
		$total['passing_count'] = $list['passing_count'];
		$this->assign('total',$total);
		$this->assign('page',$page);
		$this->display('chat_record.html');
	}
	public function clearNopass(){
		$result=Model('chat')->clearNopass();
		print_r(json_encode($result));
	}
	public function silenceUser(){
		$wxid=isset($_POST['wxid'])?$_POST['wxid']:'';
		$status=isset($_POST['status'])?intval($_POST['status']):0;
		$result=array('error'=>0,'content'=>'禁言失败');
		if($wxid&&$status){
			if(Model('chat')->silenceUser($wxid,$status)){
				$result['error'] = 1;
				$result['content'] = "禁言成功";
			}
		}
		print_r(json_encode($result));
	}
	
	public function delRecord(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('chat')->del_record($v);
			}
		}
		else
		{
			$result=Model('chat')->del_record($id);
		}
		print_r(json_encode($result));
	}
	/***********************************************禁言用户****************************************************************/
	public function silence(){
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$silence_list=Model('chat')->silence_list($page);
		$this->assign('silence_list',$silence_list['data']);
		$this->assign('user_list_page',$silence_list['pagelink']);
		$this->assign('count',$silence_list['count']);
		$this->display('silence_list.html');
	}
	
	public function delSilence(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('chat')->del_silence($v);
			}
		}
		else
		{
			$result=Model('chat')->del_silence($id);
		}
		print_r(json_encode($result));
	}
/**********************************关键词拦截******************************************************/
	public function keyword(){
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$keyword_list=Model('chat')->keyword_list($page);
		$this->assign('keyword_list',$keyword_list['data']);
		$this->assign('user_list_page',$keyword_list['pagelink']);
		$this->assign('count',$keyword_list['count']);
		$this->display('keyword_list.html');
	}
	
	public function add_keyword(){
		$sid=isset($_GET['sid'])?intval($_GET['sid']):'';
		$this->assign('action','create_keyword');
		$this->display('keyword_edit.html');
	}
	
	public function saveKeyword(){
		$info['id'] = isset($_POST['id'])?intval($_POST['id']):0;
		$info['keyword']  = isset($_POST['keyword'])?$_POST['keyword']:"";
		$result=array('error'=>1,'content'=>'关键词修改失败');
		if($info['keyword']){
			if($info['id']){//修改
				$res=Model('chat')->update($info,array("table"=>'chat_keyword',"id"=>$info['id']));
			}else{//新增
				$info['addtime'] = $_SERVER['REQUEST_TIME'];
				$res=Model('chat')->insert($info,'chat_keyword');
			}
			if($res){
				$result['error']=0;
				$result['content']='编辑成功';
			}
		}
		print_r(json_encode($result));
	}
	
	public function del_keyword(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('chat')->del_keyword($v);
			}
		}
		else
		{
			$result=Model('chat')->del_keyword($id);
		}
		print_r(json_encode($result));
	}
	/*****************************************弹幕配置************************/
	public function config(){
		$chat = Model('chat');
		$app = Model('app');
		$config = $app->get_cfg();
		$this->assign('config', $config);
		$this->display('config.html');
	}
	public function saveConfig(){
		$info['refreshtime'] = isset($_POST['refreshtime'])?intval($_POST['refreshtime']):15;
		$info['capacityPool'] = isset($_POST['capacityPool'])?intval($_POST['capacityPool']):50;
		$info['chatPool'] = isset($_POST['chatPool'])?intval($_POST['chatPool']):50;
		$info['capacityPooleach'] = isset($_POST['capacityPooleach'])?intval($_POST['capacityPooleach']):25;
		$info['isOrderBy'] = isset($_POST['isOrderBy'])?intval($_POST['isOrderBy']):0;
		$info['isRepeat'] = isset($_POST['isRepeat'])?intval($_POST['isRepeat']):0;
		$info['commentMinuteMax'] = isset($_POST['commentMinuteMax'])?intval($_POST['commentMinuteMax']):0;
		$info['commentDayMax'] = isset($_POST['commentDayMax'])?intval($_POST['commentDayMax']):0;
		$info['chatMaxNum'] = isset($_POST['chatMaxNum'])?intval($_POST['chatMaxNum']):0;
		$info['digestNum'] = isset($_POST['digestNum'])?intval($_POST['digestNum']):0;
		$info['placeholder'] = isset($_POST['placeholder'])?$_POST['placeholder']:"";
		$info['digestTime'] = isset($_POST['digestTime'])?intval($_POST['digestTime']):1;
		if(Model('chat')->saveConfig($info)){
			$result['error'] = 0;
			$result['content'] = Lang('VALID_OPERATION');
		}else{
			$result['content'] = Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	/*************************************加精*************************************/
	public function digest(){
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$digest_list=Model('chat')->digest_list($page);
		$this->assign('keyword_list',$digest_list['data']);
		$this->assign('user_list_page',$digest_list['pagelink']);
		$this->assign('count',$digest_list['count']);
		$digestNum = Model('chat')->get_cfg('digestNum');
		$this->assign('digestNum',$digestNum);
		$refresh = Model('chat')->get_cfg("visitortime");
		$now = time()-$refresh;
		$time =strtotime(date('Y-m-d H:00:00',$now));
		$timeout = $refresh-($now-$time)%$refresh;
		//获取uv与pv
		$result = Model("chat")->get_uv_pv_2M($now,$time,$refresh);
		$this->assign("pv", $result['pv']);
		$this->assign("uv", $result['uv']);
		$this->assign('timeout', $timeout);
		$this->assign("now", $now);
		$this->assign("time", $time);
		$this->assign("refresh", $refresh);
		
		//刷新时间
		$staggerTimeReal = intval(Model('chat')->get_cfg("staggerTime"));
		$timestampReal =intval(Model('chat')->get_timestamp());
		$refreshReal = intval(Model('chat')->get_refreshtime());
		$timeReal = time();
		$this->assign('timeoutReal',($refreshReal-($timeReal-($timestampReal-$staggerTimeReal))%$refreshReal));//
		$this->assign('refreshtimeReal',$refreshReal);
		$this->assign("timestampReal", $timestampReal-$staggerTimeReal);
		$this->assign("nowReal", $timeReal);
		
		
		$this->display('digest_list.html');
	}
	public function addDigest(){
		$id = isset($_POST['id'])?$_POST['id']:"";
		$res = Model('chat')->addDigest($id);
		print_r(json_encode($res));
	}
	public function failDigest(){
		$id = isset($_POST['id'])?$_POST['id']:"";
		$res = Model('chat')->failDigest($id);
		print_r(json_encode($res));
	}
	public function cleanDigestPool(){
		$result=array('error'=>0,'content'=>'清除成功');
		$res = Model('chat')->cleanDigestPool();
		if(!$res){
			$result=array('error'=>1,'content'=>'清空失败');
		}
// 		$fp = fopen(ROOT_PATH."digest.json", "w");
// 		fwrite($fp, json_encode(array()));
// 		fclose($fp);
		print_r(json_encode($result));
	}
	
	public function addDigestPool(){
		$res = Model('chat')->addDigestPool();
		print_r(json_encode($res));
	}
	public function updateDigestPool(){
		$uids = isset($_POST['uids'])?$_POST['uids']:0;
		if($uids){
			$result = Model('chat')->updateDigestPool($uids);
		}else{
			$result = array('error'=>1,'content'=>'无勾选数据');
		}
		print_r(json_encode($result));
	}
	
	public function temCheck(){
		$id = isset($_POST['id'])?$_POST['id']:0;
		if($id){
			$result = Model('chat')->updateStatus($id,1);
// 			$result = Model('chat')->updateStatus($id,0);
			if($result){
				$result = array('error'=>0,'content'=>'添加成功');
			}else{
				$result = array('error'=>1,'content'=>'添加失败');
			}
		}else{
			$result = array('error'=>1,'content'=>'添加失败');
		}
		print_r(json_encode($result));
	}
	/********************************弹幕库***********************************/
	public function library(){
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$list=Model('chat')->list_library($page);
		$this->assign('sql_list',$list['data']);
		$this->assign('list_page',$list['pagelink']);
		$total['count'] = $list['count'];
		$this->assign('page',$page);
		$refresh = Model('chat')->get_cfg("visitortime");
		$now = time()-$refresh;
		$time =strtotime(date('Y-m-d H:00:00',$now));
 		$timeout = $refresh-($now-$time)%$refresh;
 		//获取uv与pv
 		$result = Model("chat")->get_uv_pv_2M($now,$time,$refresh);
 		$this->assign("pv", $result['pv']);
 		$this->assign("uv", $result['uv']);
 		$this->assign('timeout', $timeout);
		$this->assign("now", $now);
		$this->assign("time", $time);
		$this->assign("refresh", $refresh);
		
		//刷新时间
		$staggerTimeReal = intval(Model('chat')->get_cfg("staggerTime"));
		$timestampReal =intval(Model('chat')->get_timestamp());
		$refreshReal = intval(Model('chat')->get_refreshtime());
		$timeReal = time();
		$this->assign('timeoutReal',($refreshReal-($timeReal-($timestampReal-$staggerTimeReal))%$refreshReal));//
		$this->assign('refreshtimeReal',$refreshReal);
		$this->assign("timestampReal", $timestampReal-$staggerTimeReal);
		$this->assign("nowReal", $timeReal);
		
		
		$this->display('library.html');
	}
	/*********************************PV&UV**********************************/
	public function visitor(){
		$page = isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$startTime = isset($_REQUEST['startTime'])?$_REQUEST['startTime']:' 00:00:00';
		$endTime = isset($_REQUEST['endTime'])?$_REQUEST['endTime']:' 23:59:59';
		$total['startTime'] = $startTime; 
		$total['endTime'] = $endTime; 
		$list=Model('chat')->get_visitor_record2($page,$total);
		$this->assign('sql_list',$list['data']);
		$this->assign('list_page',$list['pagelink']);
		$total['count'] = $list['count'];
		$total['uvcount'] = $list['uvcount'];
		$total['pvcount'] = $list['pvcount'];
		$this->assign('total',$total);
		$this->assign('page',$page);
		$this->display('visitor.html');
	}
	
	public function balance(){
		$result = Model("chat")->balance();
		print_r(json_encode($result));
	}
	
	
	
	/**********************************弹框管理******************************************************/
	public function group_list(){
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		
		$group_list=Model('chat')->group_list($page);
		$this->assign('group_list',$group_list['data']);
		$this->assign('group_list_page',$group_list['pagelink']);
		$this->assign('count',$group_list['count']);
		$this->display('group_list.html');
	}
	
	
	public function box_list(){
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$id=isset($_REQUEST['id'])?intval($_REQUEST['id']):1;
		$group_id=isset($_REQUEST['group_id'])?intval($_REQUEST['group_id']):1;
		$box_list=Model('chat')->box_list($page,$id);
		$this->assign('box_list',$box_list['data']);
		$this->assign('group_id',$id);
		$this->assign('box_list_page',$box_list['pagelink']);
		$this->assign('count',$box_list['count']);
		$this->display('box_list.html');
	}
	
	public function del_group(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('chat')->del_group($v);
			}
		}
		else
		{
			$result=Model('chat')->del_group($id);
		}
		print_r(json_encode($result));
	}
	
	public function del_box(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('chat')->del_box($v);
			}
		}
		else
		{
			$result=Model('chat')->del_box($id);
		}
		print_r(json_encode($result));
	}
	
	
	public function group_content()
	{
	
		$id = yf_empty(intval($_GET['id']), '', true);
		
		$ad_img = Model('chat')->getGroup($id);
		$this->assign('ad', $ad_img);
		$this->assign('id', $id);
		
		$this->assign('action', 'update_group');
		$this->display('group_content.html');
	}
	
	/**
	 * 显示版块内容编辑页
	 */
	public function box_content()
	{
		
		$id = yf_empty(intval($_GET['id']), '', true);
		$group_id = yf_empty(intval($_GET['group_id']), '', true);
		$box = Model('chat')->getBox($id);
		$group = Model('chat')->getGroup($group_id);
		$this->assign('ad', $box);
		$this->assign('group', $group);
		$this->assign('id', $id);
		$this->assign('group_id', $group_id);
		$this->assign('action', 'update_box');
		$this->display('box_content.html');
	}
	
	
	
	public function update_box()
	{
	
		$id=isset($_POST['id'])?$_POST['id']:'0';
		
		
		$info['name'] = yf_empty($_POST['name'], "弹框文字不能为空");
		$info['type'] = yf_empty($_POST['type'], '弹框类型不能为空');
		$info['group_id'] = yf_empty($_POST['group_id'], '所属弹框组不能为空');
		
		$info['content']=isset($_POST['content'])?$_POST['content']:'';
		$info['prize']=isset($_POST['prize'])?$_POST['prize']:'0';
		$info['pby']=isset($_POST['pby'])?$_POST['pby']:'0';
		$info['url']=isset($_POST['url'])?$_POST['url']:'';
		$info['pic']=isset($_POST['pic'])?$_POST['pic']:'';
		$info['is_extend_links']=isset($_POST['is_extend_links'])?$_POST['is_extend_links']:'0';
		$info['limitnums']=isset($_POST['limitnums'])?$_POST['limitnums']:'0';
		
		
		
	
		$data = array('error' => 1, 'content' => '');
		
		if($id>0)
		{
			$result = Model('chat')->update($info, array('table'=>'box','id'=>$id));
		}
		else 
		{
			$result = Model('chat')->insert($info, "box");
		}
		
		
		if($result)
		{
			$data['error']=0;
			$data['content']=Lang('SUCCESS_EDIT');
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	}
	
	
	public function update_group()
	{
	
		$id=isset($_POST['id'])?$_POST['id']:'0';
	
	
		$info['name'] = yf_empty($_POST['name'], "弹框组名称不能为空");
		$info['type'] = yf_empty($_POST['type'], '弹框组类型不能为空');
		$info['totalnums'] = yf_empty($_POST['totalnums'], '预计参与人数必须大于0');
		
		$data = array('error' => 1, 'content' => '');
	
		if($id>0)
		{
			$result = Model('chat')->update($info, array('table'=>'box_group','id'=>$id));
		}
		else
		{
			$info['addtime'] = time();
			
			$result = Model('chat')->insert($info, "box_group");
			//信息流程
			if($info['type']==1)
			{
				if($result>0)
				{
					$box['name'] = "信息框";
					$box['type'] = 1;
					$box['group_id'] = $result;
					$box['content']='';
					$box['url']='';
					$box['pic']='';
					$box['is_extend_links']=0;
					$result = Model('chat')->insert($box, "box");
				}
					
			}
			//中奖流程
			elseif($info['type']==2)
			{
				if($result>0)
				{
					$box['name'] = "谢谢参与";
					$box['type'] = 3;
					$box['group_id'] = $result;
					$box['is_extend_links']=0;
					$result = Model('chat')->insert($box, "box");
				}
			}
			//复合流程
			elseif($info['type']==3)
			{
				if($result>0)
				{
					$box['name'] = "谢谢参与";
					$box['type'] = 3;
					$box['group_id'] = $result;
					$box['is_extend_links']=0;
					Model('chat')->insert($box, "box");
						
					$box['name'] = "信息框";
					$box['type'] = 1;
					$box['group_id'] = $result;
					$box['is_extend_links']=0;
					$result = Model('chat')->insert($box, "box");
				}
			}
			}
	
	
		if($result)
		{
			$data['error']=0;
			$data['content']=Lang('SUCCESS_EDIT');
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	}
	public function box_user_list(){
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$box_id=isset($_REQUEST['box_id'])?intval($_REQUEST['box_id']):0;
		$group_id=isset($_REQUEST['group_id'])?intval($_REQUEST['group_id']):0;
		
		$box_list=Model('chat')->box_user_list($page,$box_id,$group_id);
		$boxs=Model('chat')->getBoxs($group_id);
		//var_dump($boxs);
		$this->assign('boxs',$boxs);
		$this->assign('box_list',$box_list['data']);
		$this->assign('group_id',$group_id);
		$this->assign('box_id',$box_id);
		$this->assign('box_list_page',$box_list['pagelink']);
		$this->assign('count',$box_list['count']);
		
		$this->display('box_user_list.html');
	}
	
	
	public function export_pv_uv(){
		$startTime = isset($_REQUEST['startTime'])?$_REQUEST['startTime']:' 00:00:00';
		$endTime = isset($_REQUEST['endTime'])?$_REQUEST['endTime']:' 23:59:59';
		$total['startTime'] = $startTime;
		$total['endTime'] = $endTime;
		$list=Model('chat')->export_pv_uv($total);
		$title = array('日期','时间段',"pv",'uv');
		exportData("pv&uv", $title, $list);
		//yf_daochu($title,$list,"pvuv");
	}
	
	public function export_chat_record(){
		$startTime = isset($_REQUEST['startTime'])?strtotime($_REQUEST['startTime']):mktime(0,0,0,date("m"),date("d"),date("Y"));
		$endTime = isset($_REQUEST['endTime'])?strtotime($_REQUEST['endTime']):0;
		$nickname = isset($_REQUEST['nickname'])?$_REQUEST['nickname']:'';
		$comment = isset($_REQUEST['comment'])?$_REQUEST['comment']:'';
		if($endTime!=0){
			$endTime = strtotime(date( 'Y-m-d 23:59:59', $endTime));
		}else{
			$endTime = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
		}
		$total['startTime'] = $startTime;
		$total['endTime'] = $endTime;
		$total['nickname'] = $nickname;
		$total['comment'] = $comment;
		$list=Model('chat')->export_chat_record($total);
		$title = array('昵称','openid',"评论",'评论时间');
		exportData("审核通过记录表", $title, $list);
	}
	
	public  function lookpu(){
		$time = $_REQUEST['time'];
		$list=Model('chat')->get_lookpu_day($time);
		$this->assign('pu_list',$list);
		$this->display('visitor_day_list.html');
	}
}

